package com.wanmait.wanmarecruit.webController;

import com.wanmait.wanmarecruit.entity.ApplyPosition;
import com.wanmait.wanmarecruit.entity.Position;
import com.wanmait.wanmarecruit.service.*;
import com.wanmait.wanmarecruit.service.impl.ApplyPositionServiceImpl;
import com.wanmait.wanmarecruit.service.impl.ConcernPositionServiceImpl;
import com.wanmait.wanmarecruit.entity.PositionHistory;
import com.wanmait.wanmarecruit.entity.User;
import com.wanmait.wanmarecruit.service.impl.PositionServiceImpl;
import com.wanmait.wanmarecruit.service.impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("web")
public class WebPositionDetailController {

    @Resource
    private PositionService positionService;
    @Resource
    private PositionHistoryService positionHistoryService;

    @Resource
    private ApplyPositionService applyPositionService;

    @Resource
    private UserService userService;

    @Resource
    private ConcernPositionService concernPositionService;

    @RequestMapping({"personal/positionDetail","personal/positionDetail/{id}"})
    public ModelAndView positionDetail(@PathVariable(value = "id",required = false) Integer id, HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/web/personal/positionDetail");
        Position position = positionService.getPositionById(id);
        modelAndView.addObject("position",position);

        Integer positionTypeSecondId = position.getPositionTypeSecondId();
        List<Position> positions = positionService.getFourPositionByPositionTypeSecondId(positionTypeSecondId);
        modelAndView.addObject("positions",positions);
        if (user!=null){
            PositionHistory positionHistory = new PositionHistory();
            positionHistory.setUserId(user.getId());
            positionHistory.setPositionId(id);
            positionHistoryService.insertPositionHistoryWithUserIdAndPositionId(positionHistory);
            modelAndView.addObject("applicationFlag",applyPositionService.checkApplyPositionById(id,user.getId()));
            modelAndView.addObject("concernFlag",concernPositionService.checkCollectPositionById(id,user.getId()));
        }


        return modelAndView;
    }

    //ajax申请职位（李昀逸）
    @RequestMapping("personal/applyPositionById")
    @ResponseBody
    public void applyPositionById(Integer positionId, HttpServletRequest request, HttpServletResponse response){
        User user = (User)request.getSession().getAttribute("user");
        Integer userId;
        if(user!=null){
            userId = user.getId();
        }
        else{
            userId = null;
        }

        Boolean flag = applyPositionService.checkApplyPositionById(positionId,userId);

        if(flag==true){
            //用户之前已申请
            PrintWriter out = null;
            try {
                out = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }
            out.print("true");
        }
        else{
            //insert
                applyPositionService.insertApplyPositionById(positionId,userId);
                PrintWriter out = null;
                try {
                    out = response.getWriter();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                out.print("false");
        }
    }

    //ajax收藏职位（李昀逸）
    @RequestMapping("personal/collectPositionById")
    @ResponseBody
    public void collectPositionById(Integer positionId, HttpServletRequest request, HttpServletResponse response,Boolean insertOrNot){
        User user = (User)request.getSession().getAttribute("user");
        Integer userId = user.getId();

        if(insertOrNot==true){
            concernPositionService.removeOneCollectByUserAndPositionId(positionId,userId);
            //用户之前已关注
            PrintWriter out = null;
            try {
                out = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }
            out.print("true");
        }
        else{
            //insert
            concernPositionService.insertCollectPositionById(positionId,userId);
            PrintWriter out = null;
            try {
                out = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }
            out.print("false");
        }
    }
}
